In [1]:
import altair as alt
import pandas as pd
import geopandas as gpd
In [2]:
df = pd.read_csv('population_trends.csv')
df = df[df.region != 'Ukraine']
df.head()
Out[2]:
region year rate
31 Crimea 1989 3.9
32 Crimea 1990 2.5
33 Crimea 1991 0.9
34 Crimea 1992 -0.7
35 Crimea 1993 -2.7
In [3]:
ua = gpd.read_file('ukraine.json')
In [4]:
df_2019 = df[df["year"] == 2019]
df_2019.head()
Out[4]:
region year rate
61 Crimea 2019 NaN
92 Vinnytsya 2019 -7.9
123 Volyn 2019 -2.8
154 Dnipropetrovs'k 2019 -8.9
185 Donets'k 2019 NaN
In [5]:
select_city = alt.selection_single(on = 'mouseover', nearest = False, empty = 'none')
In [6]:
alt.Chart(ua).transform_lookup(
    lookup = 'NAME_1',
    from_ = alt.LookupData(data=df_2019, 
                           key='region',
                           fields=['region', 'rate'])
).mark_geoshape().encode(
    strokeWidth=alt.condition(select_city, alt.value(3), alt.value(1)),
    stroke=alt.condition(select_city, alt.value('red'), alt.value('black')),
    color = alt.Color('rate:O'),
    tooltip = [alt.Tooltip('rate:O'), alt.Tooltip('region:N')]
).add_selection(select_city).properties(width = 900, height = 400)
Out[6]:
In [ ]:
 
In [7]:
select_city = alt.selection_single(on = 'mouseover', nearest = False, fields = ['region'], empty = 'all')
select_city_empty = alt.selection_single(on = 'mouseover', nearest = False, empty = 'none')
In [8]:
first = alt.Chart(ua).transform_lookup(
    lookup = 'NAME_1',
    from_ = alt.LookupData(data=df_2019, 
                           key='region',
                           fields=['region', 'rate'])
).mark_geoshape().encode(
    strokeWidth=alt.condition(select_city, alt.value(3), alt.value(1)),
    stroke=alt.condition(select_city_empty, alt.value('red'), alt.value('black')),
    opacity=alt.condition(
        select_city,
        alt.value(1),
        alt.value(0.1)),
    color = alt.Color('rate:O'),
    tooltip = [alt.Tooltip('rate:O'), alt.Tooltip('region:N')]
).add_selection(select_city).add_selection(select_city_empty)
In [9]:
first.properties(width = 900, height = 600)
Out[9]:
In [10]:
second = alt.Chart(df).mark_line().encode(
    x = alt.X('year:Q'),
    y = alt.Y('rate:Q'),
    detail = alt.Detail('region:N'),
    opacity=alt.condition(
        select_city,
        alt.value(1),
        alt.value(0.1)),
        tooltip = [alt.Tooltip('rate:O'), alt.Tooltip('region:N')]
    
).add_selection(select_city)
In [11]:
second.properties(width = 900, height = 600)
Out[11]:
In [12]:
alt.vconcat(alt.hconcat(first.properties(width = 400, height = 300), second.properties(width = 400, height = 300))).configure_concat(spacing = 0)
Out[12]:
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: